package com.merchant.product.quartz.job;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.merchant.product.weixin.common.bean.WxAccessToken;
import org.merchant.product.weixin.common.exception.WxErrorException;
import org.merchant.product.weixin.mp.api.WxMpConfigStorage;
import org.merchant.product.weixin.mp.api.WxMpService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.merchant.product.domain.WxToken;
import com.merchant.product.service.TokenService;

public class ScheduledJob implements Job{
	public final Logger log = Logger.getLogger(this.getClass());  
	@Autowired
	private WxMpService wxService;
	@Autowired
	private WxMpConfigStorage wxMpConfigStorage;
	@Autowired
	private TokenService tokenService;
	
    private SimpleDateFormat dateFormat() {
        return new SimpleDateFormat("HH:mm:ss");
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        //System.out.println("AAAA: The time is now " + dateFormat().format(new Date()));
        //第一步,先获取token表里面的数据
        WxToken token=tokenService.findAll();
        //System.out.println("AAAA: The time is now " + dateFormat().format(new Date()));
        //第二部,获取accesstoken值
        try {
			String accessToken=wxService.getAccessToken();
			token.setAccessToken(accessToken);
			token.setExpiresIn(7200);
			tokenService.update(token);
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
    }
}